package com.wty;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @Author wangtengyu
 * @Create 2018-05-15-14:57
 */
@Slf4j
public class CyclicBarrierTest {

    // 请求总数
    public static int Num = 6;

    // 同时并发执行的线程数
    public static int threadTotal = 2;

    public static int count = 0;

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadTotal);
        final CyclicBarrier cyclicBarrier=new CyclicBarrier(Num,()->{
            System.out.println("回调了");
        });
        for (int i = 0; i < Num ; i++) {
            executorService.execute(() -> {
                System.out.println("线程"+ Thread.currentThread().getName()+"正在写入");
                try {
                    Thread.sleep(5000L);
                    System.out.println("线程"+ Thread.currentThread().getName()+"写入数据完毕");
                    cyclicBarrier.await();
                } catch (Exception e) {
                    log.error("exception", e);
                }
                System.out.println("所有线程处理完毕");
            });
        }
        executorService.shutdown();

    }

}
